﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMouseEvent>
#include"iot.h"
#include"map.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public:
    enum Edge { None, Left, Right, Top, Bottom, TopLeft, TopRight, BottomLeft, BottomRight };
    void mousePressEvent(QMouseEvent *event) {
        if (event->button() == Qt::LeftButton) {
            QPoint pos = event->pos();

            // 标题栏拖拽判断
            if (pos.y() < 50) {
                m_dragPosition = event->globalPos() - frameGeometry().topLeft();
                setCursor(Qt::OpenHandCursor);
                event->accept();
                return;
            }

            // 边缘检测判断
            m_currentEdge = getEdge(pos);
            if (m_currentEdge != None) {
                m_bResizing = true;
                m_startGeometry = geometry();
                event->accept();
            }
        }
    }

    void mouseMoveEvent(QMouseEvent *event) {
        QPoint pos = event->pos();

        // 标题栏拖拽
        if ((event->buttons() & Qt::LeftButton) && pos.y() < 50) {
            move(event->globalPos() - m_dragPosition);
            event->accept();
            return;
        }

        // 边缘调整光标反馈
        if (!m_bResizing) {
            Edge edge = getEdge(pos);
            updateCursor(edge);
        }

        // 窗口尺寸调整
        if (m_bResizing && (event->buttons() & Qt::LeftButton)) {
            QPoint delta = event->globalPos() - m_startGeometry.topLeft();
            QRect newGeo = m_startGeometry;

            switch (m_currentEdge) {
            case Left:
                newGeo.setLeft(event->globalX());
                break;
            case Right:
                newGeo.setRight(event->globalX());
                break;
            case Top:
                newGeo.setTop(event->globalY());
                break;
            case Bottom:
                newGeo.setBottom(event->globalY());
                break;
            case TopLeft:
                newGeo.setTopLeft(event->globalPos());
                break;
            case TopRight:
                newGeo.setTopRight(event->globalPos());
                break;
            case BottomLeft:
                newGeo.setBottomLeft(event->globalPos());
                break;
            case BottomRight:
                newGeo.setBottomRight(event->globalPos());
                break;
            }
            setGeometry(newGeo);
        }
    }

    void mouseReleaseEvent(QMouseEvent *event) {
        setCursor(Qt::ArrowCursor);
        m_dragPosition = QPoint();
        m_bResizing = false;
        m_currentEdge = None;
    }

    Edge getEdge(const QPoint &pos) {
        const int width = this->width();
        const int height = this->height();

        bool left = pos.x() <= EDGE_MARGIN;
        bool right = pos.x() >= width - EDGE_MARGIN;
        bool top = pos.y() <= EDGE_MARGIN;
        bool bottom = pos.y() >= height - EDGE_MARGIN;

        if (left && top) return TopLeft;
        if (right && top) return TopRight;
        if (left && bottom) return BottomLeft;
        if (right && bottom) return BottomRight;
        if (left) return Left;
        if (right) return Right;
        if (top) return Top;
        if (bottom) return Bottom;
        return None;
    }

    void updateCursor(Edge edge) {
        switch (edge) {
        case Left:
        case Right:
            setCursor(Qt::SizeHorCursor);
            break;
        case Top:
        case Bottom:
            setCursor(Qt::SizeVerCursor);
            break;
        case TopLeft:
        case BottomRight:
            setCursor(Qt::SizeFDiagCursor);
            break;
        case TopRight:
        case BottomLeft:
            setCursor(Qt::SizeBDiagCursor);
            break;
        default:
            setCursor(Qt::ArrowCursor);
        }
    }

private slots:
    void on_getToken_clicked();

    void on_gotoMap_clicked();

    void on_exit_clicked();

    void onTimeOut();

private:
    void InitIot();
    void InitMap();

private:
    iot*Iot;
    QString token{""};
    int temperature{0};
    int humidity{0};
    float windVelocity{0};
    QString windDirect;
    bool isCar{false};
    bool isDrone{false};
    bool isLink{false};
    QString cmdBack;
    QTimer * timer;
    Map*map;
    Ui::MainWindow *ui;
    QPoint m_dragPosition;
    bool m_bResizing = false;
    QRect m_startGeometry;
    Edge m_currentEdge = None;
    const int EDGE_MARGIN = 5;  // 边缘检测范围

};
#endif // MAINWINDOW_H
